<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/29
 * Time: 下午8:20
 */

class GoodsController extends AdminBaseController {

    /**
     * 物品产出消耗报表
     */
    public function index(){
        $this->setTitle('物品产出与消耗');

        $filter = new PubFilter();
        $chart  = new PubChart();
        $grid   = new PubGrid();


        $stateOptions = array('ALL' => '物品产出与消耗');

        $lod_special_id = $this->R('special_id');
        $lodos = $this->R('os');
        $filter->addSpecialFilter(false,2);
        $filter->addOsFilter($lod_special_id,true,2)
        ->addServerFilter($lod_special_id,$lodos,true,false,2)
            ->addFilter('date', 'input', PubFunc::t('日期'), array('value' => date('Y-m-d',strtotime('yesterday')), 'date' => 'day'),2)
            ->addFilter('state', 'hidden', '', array('value' => 'ALL'),2)
            ->addExport();

        $params = $filter->get();
        
        $params = $this->indexCommon($params);
        
        $header = GoodsService::indexHeader();
        $data   = GoodsService::indexData($params,array());

        if($filter->isExport()){
            $exportData     = array();
            $exportHeader   = GoodsService::exportHeader();

            foreach($data['get'] as $v){
                $exportData[] = array(
                    'RecDate'   => $v['RecDate'],
                    'Type'      => '产出',
                    'ItemId'    => $v['ItemId'],
                    'Number'    => $v['GetNumber'],
                    'User'      => $v['GetUser'],
                    'Cnt'       => $v['GetCnt'],
                    'Rate'      => $v['GetRate']
                );
            }
            foreach($data['used'] as $v){
                $exportData[] = array(
                    'RecDate'   => $v['RecDate'],
                    'Type'      => '消耗',
                    'ItemId'    => $v['ItemId'],
                    'Number'    => $v['UsedNumber'],
                    'User'      => $v['UsedUser'],
                    'Cnt'       => $v['UsedCnt'],
                    'Rate'      => $v['UsedRate']
                );
            }

            $filename = LibExcel::getFilename('物品产出与消耗', $params);
            LibExcel::exportCsv($exportHeader, $exportData, $filename);
        }else{
            $chartConfig = GoodsService::indexChart($data, $params);

            $chart->addChart('get_chart', $chartConfig['get']['data'], array(
                'name'   => PubFunc::t("物品产出表"),
                'type'   => 'bar',
                'config'=>array(
                    'xAxis' => array('axisLabel' => array('rotate' => 40)),
                    'normal' => array(
                        'grid' => array(
                            'y2' => 100
                        )
                    )
                ),
            ));

            $chart->addChart('used_chart', $chartConfig['used']['data'], array(
                'name'   => PubFunc::t("物品消耗表"),
                'type'   => 'bar',
                'config'=>array(
                    'xAxis' => array('axisLabel' => array('rotate' => 40)),
                    'normal' => array(
                        'grid' => array(
                            'y2' => 100
                        )
                    )
                ),
            ));


            foreach($stateOptions as $k=>$v){
                if($k == $params['state']){
                    $grid->addGrid($header['get'], $data['get'], array(
                        'name'  => PubFunc::t("物品产出表"),
                        'order' => array('GetNumber', 'desc'),
                    ));
                    $grid->addGrid($header['used'], $data['used'], array(
                        'name'  => PubFunc::t("物品消耗表"),
                        'order' => array('UsedNumber', 'desc'),
                    ));
                }else{
                    $grid->addLink($k, $v);
                }
            }

            $chart->setShowType("float");
            $grid->setShowType("float");
            $this->out['filter'] = $filter->show();
            $this->out['chart'] = $chart->show();
            $this->out['grid'] = $grid->show();
            $this->out['is_ana'] = 2;
        }
    }

} 